package com.xinqi.modules.databank.remind.impl;

import com.xinqi.common.base.response.R;
import com.xinqi.common.base.user.Account;
import com.xinqi.common.core.constant.ChatRemindConstants;
import com.xinqi.common.core.enums.ChatRemindTypeEnum;
import com.xinqi.common.core.enums.CourseContentTypeEnum;
import com.xinqi.common.core.utils.PlaceholderUtils;
import com.xinqi.common.core.utils.StringUtils;
import com.xinqi.modules.chat.chat.official.client.ChatRemindContentClient;
import com.xinqi.modules.chat.chat.official.dto.req.ChatRemindContentCreateDTO;
import com.xinqi.modules.course.clazz.client.ClassClient;
import com.xinqi.modules.course.clazz.client.ClassStudentClient;
import com.xinqi.modules.course.clazz.dto.rsp.ClassResultDTO;
import com.xinqi.modules.course.clazz.dto.rsp.ClassStudentResultDTO;
import com.xinqi.modules.databank.remind.ChatRemindService;
import com.xinqi.modules.databank.v2.domain.CourseResourceEntity;
import com.xinqi.modules.databank.v2.domain.CourseResourceQuestionEntity;
import com.xinqi.modules.databank.v2.manager.CourseResourceBaseManager;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * 新奇提醒服务接口
 *
 * @Author：dzy
 * @name：ChatRemindService
 * @Date：2023/5/11 15:25
 * @Filename：ChatRemindSyncService
 */
@Service
@RequiredArgsConstructor
public class ChatRemindServiceImpl implements ChatRemindService {

    private final ChatRemindContentClient remindContentClient;

    private final ClassClient classClient;

    private final CourseResourceBaseManager resourceBaseManager;

    private final ClassStudentClient classStudentClient;

    @Override
    public void questionChatRemind(Account user, CourseContentTypeEnum contentType, CourseResourceQuestionEntity entity) {
        if (Objects.isNull(entity)) {
            return;
        }
        ClassResultDTO classResult = R.as(classClient.findClass(entity.getClassId()));
        if (Objects.isNull(classResult)) {
            return;
        }

        CourseResourceEntity resource = resourceBaseManager.getById(entity.getContentId());
        if (Objects.isNull(resource)) {
            return;
        }

        // 老师自己提问自己不发消息
        if (Objects.equals(user.getId(), resource.getUserId())) {
            return;
        }

        Map<String, Object> params = new HashMap<>();
        //评论内容分为图片和文字
        String title = "";
        if (StringUtils.isNotEmpty(entity.getTitle())) {
            title = title + entity.getTitle();
        }
        if (StringUtils.isNotEmpty(entity.getImg())) {
            title = title + "[图片]";
        }

        // 获取用户班级信息
        ClassStudentResultDTO student = R.as(classStudentClient.findByStudentId(entity.getClassId(), user.getId()));

        params.put("title", title);
        if (Objects.nonNull(student)) {
            params.put("userName", student.getStudentName() + student.getRelationship());
        } else {
            params.put("userName", user.getNickname());
        }

        params.put("courseTitle", resource.getTitle());
        params.put("courseName", classResult.getCourseName());
        params.put("className", classResult.getName());

        this.questionInitChatRemind(ChatRemindConstants.REMIND_QUESTION_TEMPLATE, params, resource.getUserId(), entity, contentType, ChatRemindTypeEnum.QUESTION);
    }

    @Override
    public void replyChatRemind(Account user, CourseContentTypeEnum contentType, CourseResourceQuestionEntity entity) {
        if (Objects.isNull(entity)) {
            return;
        }
        if (Objects.equals(user.getId(), entity.getTouid())) {
            return;
        }
        ClassResultDTO classResult = R.as(classClient.findClass(entity.getClassId()));
        if (Objects.isNull(classResult)) {
            return;
        }
        CourseResourceEntity resource = resourceBaseManager.getById(entity.getContentId());
        if (Objects.isNull(resource)) {
            return;
        }
        //评论内容分为图片和文字
        String title = "";
        if (StringUtils.isNotEmpty(entity.getTitle())) {
            title = title + entity.getTitle();
        }
        if (StringUtils.isNotEmpty(entity.getImg())) {
            title = title + "[图片]";
        }

        // 获取用户班级信息
        ClassStudentResultDTO student = R.as(classStudentClient.findByStudentId(entity.getClassId(), user.getId()));

        Map<String, Object> params = new HashMap<>();
        if (Objects.nonNull(student)) {
            params.put("userName", student.getStudentName() + student.getRelationship());
        } else {
            params.put("userName", user.getNickname());
        }
        params.put("title", title);
        params.put("courseTitle", resource.getTitle());
        params.put("courseName", classResult.getCourseName());
        params.put("className", classResult.getName());

        this.questionInitChatRemind(ChatRemindConstants.REMIND_REPLY_TEMPLATE, params, entity.getTouid(), entity, contentType, ChatRemindTypeEnum.REPLY);
    }

    @Override
    public void giveChatRemind(Account user, CourseContentTypeEnum contentType, CourseResourceQuestionEntity entity) {
        if (Objects.isNull(entity)) {
            return;
        }
        if (Objects.equals(user.getId(), entity.getUid())) {
            return;
        }
        ClassResultDTO classResult = R.as(classClient.findClass(entity.getClassId()));
        if (Objects.isNull(classResult)) {
            return;
        }
        //评论内容分为图片和文字
        String title = StringUtils.isEmpty(entity.getTitle()) ? "[图片]" : entity.getTitle();

        Map<String, Object> params = new HashMap<>();
        params.put("courseTitle", classResult.getCourseName());
        params.put("courseName", classResult.getCourseName());
        params.put("className", classResult.getName());
        params.put("title", title);

        this.questionInitChatRemind(ChatRemindConstants.REMIND_GIVE_TEMPLATE, params, entity.getUid(), entity, contentType, ChatRemindTypeEnum.GIVE);
    }

    /**
     * 评论点赞回复公共封装
     *
     * @param template
     * @param params
     * @param entity
     * @param type
     * @return
     */

    private void questionInitChatRemind(String template, Map<String, Object> params, Long userId, CourseResourceQuestionEntity entity,
                                       CourseContentTypeEnum contentType, ChatRemindTypeEnum type) {
        String content = PlaceholderUtils.replaceWithMap(template, params);

        ChatRemindContentCreateDTO remindContent = new ChatRemindContentCreateDTO();
        remindContent.setUserId(userId);
        remindContent.setType(type);
        remindContent.setContentType(contentType.getCode());
        remindContent.setContentId(entity.getContentId());
        remindContent.setClassId(entity.getClassId());
        remindContent.setContent(content);

        remindContentClient.insert(remindContent);
    }

}
